package com.nine.pesys.model.service.impl;

import com.nine.pesys.base.BaseSupportImpl;
import com.nine.pesys.model.entity.User;
import com.nine.pesys.model.service.UserService;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by L先森 on 2014/12/23.
 */
@Service
@Transactional
@SuppressWarnings("all")
public class UserServiceImpl extends BaseSupportImpl<User> implements UserService{

    @Override
    public User getByStuNum(String stuNum) {
        list =this.getSession().createCriteria(User.class).add(Restrictions.eq("stuNumber",stuNum)).list();
        User user = null;
        if (list.size()>0)
            user = list.get(0);
        return user;
    }

    @Override
    public List<User> getByCondition(User user) {
            String sql = "from User u where 1=1";
            if(user.getStuNumber()!=null)
                sql += " and u.stuNumber = '"+user.getStuNumber()+"'";
            if (user.getId()!=null)
                sql += " and u.id ="+user.getId();
            if (user.getName()!=null)
                sql += " and u.name = '"+user.getName()+"'";
            if(user.getEmail()!=null)
                sql += " and u.email = '"+ user.getEmail()+"'";
            if(user.getPassword()!=null)
                sql += " and u.password = '"+ user.getPassword()+"'";
            if(user.getGender()!=null)
                sql += " and u.gender = '"+ user.getGender()+"'";
            System.out.println(sql+"=========这是sql语句==========");
            return getSession().createQuery(sql).list();

    }

    @Override
    public List<User> getByPage(Integer pageSize, Integer pageNo) {
        System.out.println(pageSize+"======"+pageNo);
        list = this.getSession().createCriteria(User.class)
                .setFirstResult((pageNo-1)*pageSize)
                .setMaxResults(pageSize)
                .list();
        return list;
    }
}
